# -*- coding:utf-8 -*-
from django import template
from django.utils.safestring import mark_safe
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
# 注意templatetags下有__init__.py


register = template.Library()


# 瀑布流分列显示的tag
@register.simple_tag
def student_detail1(item, counter, allcount, remainder):
	temp = """
			 <img src="/{}">
			 <p>姓名：{}</p>
			 <p>公司：{}</p>
			 <p>薪资：{}</p>
			 <p>感谢信：{}</p>
	"""
	if counter % allcount == remainder:

		temp = temp.format(item['student__pic'],
						   item['student__name'],
						   item['student__company'],
						   item['student__salary'],
						   item['letter_of_thanks'])

		return mark_safe(temp)
	else:
		return ""


# 组合查询a标签中href的tag
@register.simple_tag
def action(current_url, item, index):
	# videos-0-0-1.html
	# item: id name
	# 重组url，先切分--再各自赋值--再join
	url_part_list = current_url.split('-')
	# print url_part_list[index]
	if url_part_list[index].strip('.html/') == str(item['id']):  # 比较url中的id与item['id']，相同则置于选中状态
		class1 = 'active'
	else:
		class1 = ''
	url_part_list[index] = str(item['id'])
	# print url_part_list
	href = '-'.join(url_part_list)
	# print href
	if '.html/' not in href:  # 解决切片最后一个带.html的
		href += '.html/'
	else:
		pass
	# temp = "<a class='%s' href='%s'>%s</a>" % (class1, href, item['name'])
	temp = "<a class='{0}' href='{1}'>{2}</a>" .format(class1, href, item['name'])

	return mark_safe(temp)


@register.simple_tag
def action_all(current_url, index):
	url_part_list = current_url.split('-')
	if url_part_list[index].strip('.html/') == str(0):
		class1 = 'active'
	else:
		class1 = ''
	url_part_list[index] = str(0)
	href = '-'.join(url_part_list)
	if '.html/' not in href:
		href += '.html/'
	else:
		pass

	temp = "<a class='{0}' href='{1}'>全部</a>" .format(class1, href)

	return mark_safe(temp)




